package com.flycms.module.qasystem.service;

import java.util.Date;
import java.util.List;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.flycms.module.comment.dao.AnswersCommentDao;
import com.flycms.module.qasystem.dao.AnswersDao;
import com.flycms.module.qasystem.dao.QuestionsDao;
import com.flycms.module.qasystem.model.Answers;

/**
 * 回答类Service
 *
 * <br/>Copyright 2020
 * <br/>All rights reserved
 * @author 沁言訫語
 * @version 2020年2月17日
 * @Date 2020年2月17日
 */

@Service
public class AnswersService {

    @Autowired
    protected AnswersDao answersDao;
    
    @Autowired
    protected AnswersCommentDao answersCommentDao;
    
    @Autowired
    protected QuestionsDao questionsDao;
    
    public int addAnswer(Answers answer) throws Exception {
        int c = answersDao.addAnswer(answer);
        Integer answerCount = questionsDao.queryQuestionAnswerCount(answer.getQuestionId());
        if (answerCount == null) {
            answerCount = 1;
        } else {
            answerCount++;
        }
        questionsDao.updateQuestionAnswerCount(answerCount, answer.getQuestionId());
        return c;
    }
    
    @Transactional
    public int editAnswer(Long id, String content, Integer status, Date updateTime, Long updateUserId) throws Exception {
        Answers answer = new Answers();
        answer.setId(id);
        answer.setStatus(status);
        answer.setUpdateTime(updateTime);
        answer.setUpdateUserId(updateUserId);
        return answersDao.editAnswer(answer);
    }
    
    public List<Answers> getAnswerList(Long proposeUserId, Long questionId, Integer status,
            String orderby, String order, int pageIndex, int pageSize) throws Exception {
        orderby = "create_time";
        order = "desc";
        return answersDao.getAnswerList(proposeUserId, questionId, status, orderby, order, pageIndex, pageSize);
    }
    
    public int getAnswerCount(Long proposeUserId, Long questionId, Integer status) throws Exception {
        return answersDao.getAnswerCount(proposeUserId, questionId, status);
    }
    
    public List<Answers> getQuestionAnswerList(Long proposeUserId, Long questionId, Integer status,
            int pageIndex, int pageSize) throws Exception {
        List<Answers> ansList = answersDao.getQuestionAnswerList(questionId, status, pageIndex, pageSize);
//        List<Long> idList = new ArrayList<>();
        for (Answers a : ansList) {
//            idList.add(a.getId());
            a.setComments(answersCommentDao.getCommentList(a.getId(), null, status, "create_time", "desc", 0, 3));
            a.setCommentCount(answersCommentDao.getCommentCount(a.getId(), null, status));
        }
        return ansList;
    }
    
    public int getQuestionAnswerCount(Long proposeUserId, Long questionId, Integer status) throws Exception {
        return answersDao.getQuestionAnswerCount(questionId, status);
    }
    
    public Answers queryAnswerById(Long id, Integer status) throws Exception {
        return answersDao.queryAnswerById(id, status);
    }
    
    public int removeAnswerById(Long id, Long userId) throws Exception {
        return answersDao.removeAnswerById(id, userId);
    }
    
    @Transactional
    public int auditAnswer(Long id, Integer status, String opinion, Date updateTime, Long updateUserId)
            throws Exception {
        Answers answer = new Answers();
        answer.setId(id);
        answer.setStatus(status);
        answer.setOpinion(opinion);
        answer.setUpdateTime(updateTime);
        answer.setUpdateUserId(updateUserId);
        return answersDao.editAnswer(answer);
    }
    
}
